/*
' Copyright (c) 2012-2013  Kyriakos Lesgidis
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
*/

using System;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Security;
using System.Data;
using net.diadrasis.CameraSlideshow.Components;
using System.Collections;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;


namespace net.diadrasis.CameraSlideshow
{

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The View class displays the content
    /// 
    /// Typically your view control would be used to display content or functionality in your module.
    /// 
    /// View may be the only control you have in your project depending on the complexity of your module
    /// 
    /// Because the control inherits from CameraSlideshowModuleBase you have access to any custom properties
    /// defined there, as well as properties from DNN such as PortalId, ModuleId, TabId, UserId and many more.
    /// 
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class View : CameraSlideshowModuleBase, IActionable
    {
        public DataTable images = new DataTable();
        public Boolean isEmpty = false;

        #region Properties

        public string pAlignment = "center", pTransitions = "random", pHeight = "300px", pLoader = "pie", pLoaderColor = "#eeeeee", pLoaderBgColor = "#222222",
            pPiePosition = "rightTop";
        public bool pAutoAdvance = true, pHover = true, pNavigation = true, pNavigationHover = true, pPlayPause = true, pPauseOnClick = true,
            pPortrait = false, pThumbnails = true, pPagination = true;
        public float pLoaderOpacity = .8f;
        public int pPieDiameter = 38, pTime = 7000, pTransPeriod = 1500;

        #endregion


        #region Event Handlers

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }


        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Load runs when the control is loaded
        /// </summary>
        /// -----------------------------------------------------------------------------
        private void Page_Load(object sender, System.EventArgs e)
        {
            try
            {
                //this.LinkButton1.PostBackUrl = EditUrl("Edit");

                DataTable dt = new DataTable();
                dt.Load(cObjectController.getObject(ModuleId));

                if (dt.Rows.Count > 0)
                {                   
                    pAlignment = dt.Rows[0]["objectAlignment"].ToString();                                                
                    pAutoAdvance = (dt.Rows[0]["objectAutoAdvance"].ToString() == "True");
                    pTransitions = dt.Rows[0]["objectTransitions"].ToString();
                    pHeight = dt.Rows[0]["objectHeight"].ToString();
                    pHover = (dt.Rows[0]["objectHover"].ToString() == "True");
                    pLoader = dt.Rows[0]["objectLoader"].ToString();
                    pLoaderColor = dt.Rows[0]["objectLoaderColor"].ToString();
                    pLoaderBgColor = dt.Rows[0]["objectLoaderBgColor"].ToString();
                    pLoaderOpacity = (float) Convert.ToDouble(dt.Rows[0]["objectLoaderOpacity"].ToString());
                    pNavigation = (dt.Rows[0]["objectNavigation"].ToString() == "True");
                    pNavigationHover = (dt.Rows[0]["objectNavigationHover"].ToString() == "True");
                    pPlayPause = (dt.Rows[0]["objectPlayPause"].ToString() == "True");
                    pPauseOnClick = (dt.Rows[0]["objectPauseOnClick"].ToString() == "True");
                    pPieDiameter = Convert.ToInt32(dt.Rows[0]["objectPieDiameter"].ToString());
                    pPiePosition = dt.Rows[0]["objectPiePosition"].ToString();
                    pPortrait = (dt.Rows[0]["objectPortrait"].ToString() == "True");
                    pThumbnails = (dt.Rows[0]["objectThumbnails"].ToString() == "True");
                    pPagination = (dt.Rows[0]["objectPagination"].ToString() == "True");                    
                    pTime = Convert.ToInt32(dt.Rows[0]["objectTime"].ToString());
                    pTransPeriod = Convert.ToInt32(dt.Rows[0]["objectTransPeriod"].ToString());


                    images.Load(cMediaObjectController.getMediaByObject(ModuleId));

                    if (images.Rows.Count == 0)
                        isEmpty = true;

                }                              
                else
                {
                    cObject o = new cObject();
                                        
                    o.objectAlignment = "topLeft";
                    o.objectAutoAdvance = true;
                    o.objectTransitions = "random";
                    o.objectHeight = "300px";
                    o.objectHover = true;
                    o.objectLoader = "pie";
                    o.objectLoaderColor = "#eeeeee"; 
                    o.objectLoaderBgColor = "#222222"; 
                    o.objectLoaderOpacity = .8f;
                    o.objectNavigation = true;
                    o.objectNavigationHover = true;
                    o.objectPlayPause = true;
                    o.objectPauseOnClick = true;
                    o.objectPieDiameter = 38;
                    o.objectPiePosition = "topRight";
                    o.objectPortrait = false;
                    o.objectThumbnails = false;
                    o.objectPagination = true;
                    o.objectTime = 5000;
                    o.objectTransPeriod = 1500;
                    o.moduleId = ModuleId;

                    cObjectController.addObject(o);
                    
                    isEmpty = true;

                    //Page.Response.Redirect(Request.RawUrl);
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #endregion

        public string getImageUrl(string fID)
        {
            if (fID != "")
            {
                FileController fileCnt = new FileController();
                FileInfo objFile = fileCnt.GetFileById(Convert.ToInt32(fID.Substring(7)), PortalId);

                return Globals.GetPortalSettings().HomeDirectory + objFile.Folder + objFile.FileName;
            }
            else
            {
                return "";
            }
        }

        #region Optional Interfaces

        public ModuleActionCollection ModuleActions
        {
            get
            {
                ModuleActionCollection Actions = new ModuleActionCollection();
                Actions.Add(GetNextActionID(), Localization.GetString("EditModule", this.LocalResourceFile), "Edit Images", "", "", EditUrl(), false, SecurityAccessLevel.Edit, true, false);
                //Actions.Add(GetNextActionID(), "Rearrange Images", "", "", "~/DesktopModules/CameraSlideshow/images/Add-2.png", UrlUtils.PopUpUrl(Globals.NavigateURL("Rearrange", "mid=" + this.ModuleId), this, PortalSettings, true, false, 500, 800), false, SecurityAccessLevel.Edit, true, false);
                return Actions;
            }
        }

        #endregion

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            //Response.Redirect(EditUrl("Edit"));
            //LinkButton1.Attributes.Add("onclick", "return " + UrlUtils.PopUpUrl(Globals.NavigateURL("Edit", "mid=" + this.ModuleId), this, PortalSettings, true, false, 390, 670));
        }

        public string getThumbPath(string filePath)
        {
            string fileName = filePath.Substring(0, filePath.LastIndexOf("."));
            string extention = filePath.Substring(filePath.LastIndexOf("."), filePath.Length - filePath.LastIndexOf("."));

            return fileName + "-thumb" + extention;
        }

    }

}
